<?php
    /* 
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /**
    * Description of friend
    *
    * @author Gidaff_02
    */
    class Friend extends Doggy_Flirt_FrontEnd_Controller{
        function Friend(){
            parent::Doggy_Flirt_FrontEnd_Controller();
        }
        function send_friend_request($friend_id){
            is_user();
            $this->_data['friend_detail'] = $this->Friend_m->get_friend_details($friend_id);
            $this->display_view('friends/request');
        }
        function request(){
            is_user();
            if($this->input->post('submit')){
                $me = $this->_data['account_id'];
                $friend_id = $this->input->post('friend_id');
                $data = array(
                'from_account'=>$me,
                'to_account'=>$friend_id
                );
                $this->_data['request_sent'] = $this->Friend_m->send_request($data);
                if($this->_data['request_sent']>0){
                    $this->session->set_flashdata('suscess','Votre de mande a été envoyé !');
                    redirect('friend/reuqest_success');    
                } else{
                    $this->display_view('friends/request_error');
                }
            }
        }
        function reuqest_success(){
            $this->display_view('friends/request_success');
        }
        function accept($id){
            is_user();
            $this->_data['friend_details'] =$this->Friend_m->get_friend_details($id);
            if($this->input->post('submit')){
                $data = array(
                'account_id'=>$this->_data['account_id'],
                'friend_id'=>$id
                );
                $data1= array(
                'account_id'=>$id,
                'friend_id'=>$this->_data['account_id']
                );

                $this->Friend_m->add_friend($data);
                $this->Friend_m->add_friend($data1);
                $this->Friend_m->delete_friend_request($id,$this->_data['account_id']);
                $this->Friend_m->delete_friend_request($this->_data['account_id'],$id);
                redirect('/');
            } else if($this->input->post('deny')){
                    //Delete request
                    //send deny info to sender
                } else{
                    $this->display_view('friends/accept');
            }
        }
        function list_all_request(){
            is_user();
            $per_page = $this->uri->segment(3)!=''?$this->uri->segment(3):20;
            $off_set = $this->uri->segment(4)!=''?$this->uri->segment(4):0;
            $config['base_url'] = base_url() . 'index.php/friend/list_all_request/' . $per_page . '/';
            $config['per_page'] = $per_page;
            $config['uri_segment'] = 4;
            $config['total_rows'] = $this->Friend_m->count_friend_request($this->_data['account_id']);
            if ($off_set > 0 && $off_set == $config['total_rows']) {
                $off_set = $off_set - $per_page;
            }
            $this->_data['requests'] = $this->Friend_m->list_friend_request($this->_data['account_id'],$per_page,$off_set);
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->display_view('friends/list_request');
        }
        function list_friends(){
            is_user();
            $id = $this->_data['account_id'];
            $per_page = $this->uri->segment(3)!=''?$this->uri->segment(3):20;
            $off_set = $this->uri->segment(4)!=''?$this->uri->segment(4):0;
            $config['base_url'] = base_url() . 'index.php/friend/list_friends/' . $per_page . '/';
            $config['per_page'] = $per_page;
            $config['uri_segment'] = 4;
            $config['total_rows'] = $this->Friend_m->count_friends($this->_data['account_id']);
            if ($off_set > 0 && $off_set == $config['total_rows']) {
                $off_set = $off_set - $per_page;
            }
            $this->_data['friends'] = $this->Friend_m->get_friends($this->_data['account_id'],$per_page,$off_set);
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->display_view('friends/list_all_friends');
        }
        function random_accounts(){
            $id = $this->_data['account_id'];
            $per_page = $this->uri->segment(3)!=''?$this->uri->segment(3):20;
            $off_set = $this->uri->segment(4)!=''?$this->uri->segment(4):0;
            $config['base_url'] = base_url() . 'index.php/friend/random_accounts/' . $per_page . '/';
            $config['per_page'] = $per_page;
            $config['uri_segment'] = 4;
            $config['total_rows'] = $this->Friend_m->count_random_account($this->_data['account_id']);
            if ($off_set > 0 && $off_set == $config['total_rows']) {
                $off_set = $off_set - $per_page;
            }
            $this->_data['no_friends'] = $this->Friend_m->list_random_account($this->_data['account_id'],$per_page,$off_set);
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->display_view('friends/list_random_not_friends');
        }
    }
?>
